﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace NJL.Web
{
    public partial class RegisterVisitor :BasePage
    {
        NJL.BLL.NJL_PERSON bllPerson = new BLL.NJL_PERSON();
        Model.NJL_PERSON modelPerson = new Model.NJL_PERSON();
        NJL.BLL.NJL_USER bllUser = new BLL.NJL_USER();
        Model.NJL_USER modelUser;
        NJL.BLL.NJL_CS_LIKES bllLikes = new BLL.NJL_CS_LIKES();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataSet ds = bllLikes.GetList("");
                cbxlLove.DataSource = ds;
                cbxlLove.DataTextField = "AREA_NAME";
                cbxlLove.DataValueField = "AREA_CODE";
                cbxlLove.DataBind();
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (bllPerson.isUresRegister(UserId).Tables[0].Rows.Count > 0)
            {
                Royalsoft.Common.MessageBox.Show(this, "该账户已注册过，请重新注册新的账户");
                return;
            }
                        
            modelPerson.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
            modelPerson.REAL_NAME = txtName.Text.Trim();
            modelPerson.SEX = rblSex.SelectedValue;
            modelPerson.ID_TYPE = ddlType.SelectedValue;
            modelPerson.ID_CODE = txtNumber.Text;
            if (txtBirthday.Text != "")
            {
                modelPerson.BIRTHDAY = Convert.ToDateTime(txtBirthday.Text);
            }
            modelPerson.DEGREE = txtEducation.Text;
            string likes = string.Empty;
            foreach (ListItem li in cbxlLove.Items)
            {
                likes += li.Value + ",";
            }
            if (!string.IsNullOrEmpty(likes))
            {
                modelPerson.LIKES = likes.Substring(0,likes.Length-1);
            }        
            modelPerson.CREATE_DATE_TIME = DateTime.Now;
            modelPerson.AREA_CODE = WebSiteCode;
            modelPerson.NJL_SYS_BASE_ID = WebSiteCode;
            modelPerson.NJL_USER_ID = UserId;

            if (bllPerson.Add(modelPerson))
            {
                modelUser = bllUser.GetModel(UserId);
                if (modelUser != null)
                {
                    modelUser.TYPE_CODE = "2";
                    Session.Add("usertype", "2");
                    if (bllUser.Update(modelUser))
                    {
                        Response.Redirect("~/AdminVisitor/Default.aspx");
                    }
                }
            }
        }
        /**/
    }
}